import 'package:number_spark_flutter/pages/ns_mine/ns_mine_logic.dart';
import 'package:number_spark_flutter/style/ns_style.dart';
import 'package:number_spark_flutter/utils/ns_avatar_utils.dart';
import 'package:number_spark_flutter/utils/ns_log_report_utils.dart';
import 'package:number_spark_flutter/utils/ns_picker_util.dart';
import 'package:number_spark_flutter/utils/ns_user_utils.dart';
import 'package:number_spark_flutter/widgets/ns_bottom_action_sheet.dart';
import 'package:number_spark_flutter/widgets/brick/ns_brick.dart';
import 'package:number_spark_flutter/widgets/ns_app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:number_spark_flutter/widgets/ns_white_app_bar.dart';

import 'ns_setting_user_logic.dart';

class NSSettingUserPage extends StatelessWidget {
  NSSettingUserPage({Key? key}) : super(key: key);

  final logic = Get.put(NSSettingUserLogic());
  final mineLogic = Get.find<NSMineLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          const NSImage(
            assetImage: 'img_bg_main.png',
            width: double.infinity,
            height: double.infinity,
            fit: BoxFit.fill,
          ),
          Column(
            children: [
              const NSWhiteAppBar(''),
              Expanded(
                child: SingleChildScrollView(
                  child: NSColumn(
                    onTap: () {
                      FocusScope.of(context).requestFocus(FocusNode());
                    },
                    margin: EdgeInsets.only(left: 30.w, right: 30.w),
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      GetBuilder<NSMineLogic>(builder: (logic) {
                        return Center(
                          child: GestureDetector(
                            child: Stack(
                              alignment: Alignment.bottomRight,
                              clipBehavior: Clip.none,
                              children: [
                                NSImage(
                                  httpImage: NSUserUtils.getIsChangedAvatar() ? logic.userInfoModel.avatarThumbUrl : '',
                                  assetPlaceHolder: 'icon_default_avatar.png',
                                  width: 136.w,
                                  height: 136.w,
                                  radius: 100.w,
                                  color: Colors.grey.withOpacity(0.8),
                                  strokeColor: Colors.white,
                                  strokeWidth: 2.w,
                                ),
                                Positioned(
                                  right: -8.w,
                                  bottom: -8.w,
                                  child: NSImage(
                                    assetImage: 'icon_avatar_take.png',
                                    width: 48.w,
                                  ),
                                )
                              ],
                            ),
                            onTap: () {
                              NSBottomActionSheet.show(context, ['Take a photo', 'Choose from album'], callBack: (index) {
                                if (index == 0) {
                                  AvatarUtils.onCameraPicker(context, onSuccess: (url) async {
                                    await NSUserUtils.setIsChangedAvatar(true);
                                    logic.userInfoModel.avatarThumbUrl = url;
                                    logic.update();
                                    NSLogReportUtils.stat(page: NSLogPages.nsEditAvatar);
                                  });
                                } else {
                                  AvatarUtils.onChoosePhoto(context, onSuccess: (url) async {
                                    await NSUserUtils.setIsChangedAvatar(true);
                                    logic.userInfoModel.avatarThumbUrl = url;
                                    logic.update();
                                    NSLogReportUtils.stat(page: NSLogPages.nsEditAvatar);
                                  });
                                }
                              });
                            },
                          ),
                        );
                      }),
                      NSText(
                        margin: EdgeInsets.only(top: 32.h, left: 24.w, bottom: 8.h),
                        text: 'Nick Name',
                        textColor: Colors.white,
                        fontSize: 20.sp,
                        fontFamily: 'Modak-Regular',
                      ),
                      GetBuilder<NSSettingUserLogic>(builder: (logic) {
                        return NSRow(
                          width: 315.w,
                          height: 56.w,
                          color: kAppColor('#20000000'),
                          radius: 28.w,
                          children: [
                            Expanded(
                              child: TextField(
                                controller: logic.nickNameController,
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 20.sp,
                                  fontFamily: 'Modak-Regular',
                                ),
                                decoration: InputDecoration(
                                  hintText: '',
                                  hintStyle: TextStyle(
                                    color: kAppColor('#EEEEEE').withOpacity(0.3),
                                    fontSize: 20.sp,
                                    fontFamily: 'Modak-Regular',
                                  ),
                                  border: const OutlineInputBorder(borderSide: BorderSide.none),
                                ),
                                onChanged: (value) {
                                  logic.update();
                                },
                                inputFormatters: [LengthLimitingTextInputFormatter(15)],
                              ),
                            ),
                            if (logic.nickNameController.text.isEmpty)
                              NSImage(
                                margin: EdgeInsets.only(right: 20.w),
                                assetImage: 'icon_mine_edit.png',
                                width: 32.w,
                                onTap: () {},
                              ),
                          ],
                        );
                      }),
                      NSText(
                        margin: EdgeInsets.only(top: 20.h, left: 24.w, bottom: 8.h),
                        text: 'Date of Birth',
                        textColor: Colors.white,
                        fontSize: 20.sp,
                        fontFamily: 'Modak-Regular',
                      ),
                      GetBuilder<NSMineLogic>(builder: (logic) {
                        return NSRow(
                          width: 315.w,
                          height: 56.w,
                          color: kAppColor('#20000000'),
                          radius: 28.w,
                          children: [
                            Expanded(
                              child: NSText(
                                alignment: Alignment.center,
                                textAlign: TextAlign.center,
                                text: logic.userInfoModel.birthday,
                                fontSize: 20.sp,
                                fontFamily: 'Modak-Regular',
                                textColor: Colors.white,
                              ),
                            ),
                            if (logic.userInfoModel.birthday?.isEmpty == true)
                              NSImage(
                                margin: EdgeInsets.only(right: 20.w),
                                assetImage: 'icon_mine_edit.png',
                                width: 32.w,
                                onTap: () {},
                              ),
                          ],
                          onTap: () {
                            NSPickerUtil.showPickerDateTime(context, 7, (picker, selected) {
                              logic.userInfoModel.birthday = picker.adapter.text.substring(0, 10);
                              logic.update();
                            }, maxDateTime: DateTime(DateTime.now().year - 18, DateTime.now().month, DateTime.now().day), minDateTime: DateTime(DateTime.now().year - 100));
                          },
                        );
                      }),
                      NSText(
                        margin: EdgeInsets.only(top: 20.h, left: 24.w, bottom: 8.h),
                        text: 'Country',
                        textColor: Colors.white,
                        fontSize: 20.sp,
                        fontFamily: 'Modak-Regular',
                      ),
                      GetBuilder<NSSettingUserLogic>(builder: (logic) {
                        return NSRow(
                          width: 315.w,
                          height: 56.w,
                          color: kAppColor('#20000000'),
                          radius: 28.w,
                          children: [
                            Expanded(
                              child: TextField(
                                controller: logic.countryController,
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 20.sp,
                                  fontFamily: 'Modak-Regular',
                                ),
                                decoration: InputDecoration(
                                  hintText: '',
                                  hintStyle: TextStyle(
                                    color: kAppColor('#EEEEEE').withOpacity(0.3),
                                    fontSize: 20.sp,
                                    fontFamily: 'Modak-Regular',
                                  ),
                                  border: const OutlineInputBorder(borderSide: BorderSide.none),
                                ),
                                onChanged: (value) {
                                  logic.update();
                                },
                                inputFormatters: [LengthLimitingTextInputFormatter(15)],
                              ),
                            ),
                            if (logic.countryController.text.isEmpty)
                              NSImage(
                                margin: EdgeInsets.only(right: 20.w),
                                assetImage: 'icon_mine_edit.png',
                                width: 32.w,
                                onTap: () {},
                              ),
                          ],
                        );
                      }),
                      NSStack(
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(top: 48.h, left: 64.w, right: 64.w),
                        children: [
                          NSImage(
                            assetImage: 'img_bg_mine_btn.png',
                            width: 246.w,
                            height: 59.w,
                            fit: BoxFit.fill,
                            radius: 29.w,
                          ),
                          NSText(
                            text: 'Submit',
                            fontSize: 24.sp,
                            fontFamily: 'Modak-Regular',
                            foreground: Paint()
                              ..style = PaintingStyle.stroke
                              ..strokeWidth = 1.5
                              ..color = kAppColor('#FF4BA611'),
                          ),
                          NSText(
                            text: 'Submit',
                            textColor: Colors.white,
                            fontSize: 24.sp,
                            fontFamily: 'Modak-Regular',
                          ),
                        ],
                        onTap: () {
                          logic.doClickSubmit();
                        },
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
